package com.ruoyi.business.service.koc.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.koc.TbKocLevel;
import com.ruoyi.business.domain.koc.TbKocTask;
import com.ruoyi.business.domain.koc.TbKocTaskType;
import com.ruoyi.business.domain.koc.TbUser;
import com.ruoyi.business.domain.vo.TbUserKocTaskVO;
import com.ruoyi.business.mapper.koc.TbKocTaskTypeMapper;
import com.ruoyi.business.service.koc.ITbKocLevelService;
import com.ruoyi.business.service.koc.ITbKocTaskService;
import com.ruoyi.business.service.koc.ITbKocTaskTypeService;
import com.ruoyi.business.service.koc.ITbUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * koc 任务类别服务层实现
 *
 * @author chenran
 */
@RequiredArgsConstructor
@Service
public class TbKocTaskTypeServiceImpl extends ServiceImpl<TbKocTaskTypeMapper, TbKocTaskType> implements ITbKocTaskTypeService {

    @Resource
    private ITbUserService tbUserService;

    @Resource
    private ITbKocLevelService tbKocLevelService;

    @Resource
    private ITbKocTaskService tbKocTaskService;

    @Override
    public List<TbKocTaskType> queryAll() {
        List<TbKocTaskType> tbKocTaskTypes = this.baseMapper.selectList();
        List<TbKocTask> tbKocTaskList = tbKocTaskService.list();
        Map<Long, List<TbKocTask>> taskMap = tbKocTaskList.stream().collect(Collectors.groupingBy(TbKocTask::getTaskTypeId));
        tbKocTaskTypes.forEach(taskTypes -> {
            // 查询任务类型下的任务
            taskTypes.setTbKocTasks(taskMap.get(taskTypes.getId()));
        });
        return tbKocTaskTypes;
    }

    @Override
    public TbKocTaskType queryById(Long id) {
        TbKocTaskType tbKocTaskType = this.getById(id);
        if (ObjectUtils.isNotEmpty(tbKocTaskType)) {
            List<TbKocTask> tbKocTasks = tbKocTaskService.getBaseMapper().selectList(Wrappers.lambdaQuery(TbKocTask.class).eq(TbKocTask::getTaskTypeId, tbKocTaskType.getId()));
            if (CollectionUtils.isNotEmpty(tbKocTasks)) {
                tbKocTaskType.setTbKocTasks(tbKocTasks);
            }
        }
        return tbKocTaskType;
    }

    @Override
    public TbUserKocTaskVO queryByUserKocLevel(Long userId) {
        TbUser userInfo = tbUserService.getBaseMapper().selectById(userId);
        List<TbKocTask> tbKocTaskList = tbKocTaskService.list();
        Map<Long, List<TbKocTask>> taskMap = tbKocTaskList.stream().collect(Collectors.groupingBy(TbKocTask::getTaskTypeId));
        TbUserKocTaskVO tbUserKocTaskVO = TbUserKocTaskVO.builder().build();
        if (Objects.equals(userInfo.getIsKoc(), Constants.TRUE_1)) {
            TbKocLevel tbKocLevel = tbKocLevelService.getById(userInfo.getKocLevelId());
            // 用户koc等级必做任务
            List<String> mustKocTaskTypes = Optional.ofNullable(tbKocLevel.getMustKocTaskType())
                .map(str -> str.split(","))
                .map(Arrays::stream)
                .map(stream -> stream.collect(Collectors.toList()))
                .orElse(Collections.emptyList());
            if (CollectionUtils.isNotEmpty(mustKocTaskTypes)) {
                List<TbKocTaskType> mustKocTaskTypeList = this.list(Wrappers.lambdaQuery(TbKocTaskType.class).in(TbKocTaskType::getId, mustKocTaskTypes));
                mustKocTaskTypeList.forEach(taskType -> {
                    taskType.setTbKocTasks(taskMap.get(taskType.getId()));
                });
                tbUserKocTaskVO.setMustKocTaskTypes(mustKocTaskTypeList);
            }
            // 用户koc等级非必做任务
            List<String> requestKocTaskTypes = Optional.ofNullable(tbKocLevel.getRequestKocTaskType())
                .map(str -> str.split(","))
                .map(Arrays::stream)
                .map(stream -> stream.collect(Collectors.toList()))
                .orElse(Collections.emptyList());
            if (CollectionUtils.isNotEmpty(requestKocTaskTypes)) {
                List<TbKocTaskType> requestKocTaskTypeList = this.list(Wrappers.lambdaQuery(TbKocTaskType.class).in(TbKocTaskType::getId, requestKocTaskTypes));
                requestKocTaskTypeList.forEach(taskType -> {
                    taskType.setTbKocTasks(taskMap.get(taskType.getId()));
                });
                tbUserKocTaskVO.setRequestKocTaskTypes(requestKocTaskTypeList);
            }
            // 用户koc等级提升必做任务 upRequestKocTaskTypes
            List<String> upRequestKocTaskTypes = Optional.ofNullable(tbKocLevel.getUpRequestKocTaskType())
                .map(str -> str.split(","))
                .map(Arrays::stream)
                .map(stream -> stream.collect(Collectors.toList()))
                .orElse(Collections.emptyList());
            if (CollectionUtils.isNotEmpty(upRequestKocTaskTypes)) {
                List<TbKocTaskType> upRequestKocTaskTypeList = this.list(Wrappers.lambdaQuery(TbKocTaskType.class).in(TbKocTaskType::getId, upRequestKocTaskTypes));
                upRequestKocTaskTypeList.forEach(taskType -> {
                    taskType.setTbKocTasks(taskMap.get(taskType.getId()));
                });
                tbUserKocTaskVO.setUpRequestKocTaskTypes(upRequestKocTaskTypeList);
            }
        } else {
            throw new ServiceException("非koc用户");
        }
        return tbUserKocTaskVO;
    }
}
